<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    
     <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
							<li>
								批次号：<input type="text" name="sn"/>
							</li>

							<li>
								计划名称：<input type="text" name="planName"/>
							</li>

							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			
	        <div class="btn-group-sm hidden-xs" id="toolbar" role="group">
				<a class="btn btn-danger btn-del btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="jmeter:jmeterTestPlanMetric:remove">
					<i class="fa fa-remove"></i> 删除
				</a>
				<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="jmeter:jmeterTestPlanMetric:export">
						<i class="fa fa-download"></i> 导出
				 </a>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>

	 <script th:src="@{/js/ws/sockjs.js}"></script>
	 <script th:src="@{/js/ws/stomp.js}"></script>
	 <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('jmeter:jmeterTestPlanMetric:edit')}]];
        var removeFlag = [[${@permission.hasPermi('jmeter:jmeterTestPlanMetric:remove')}]];
        var prefix = ctx + "jmeter/jmeterTestPlanMetric/";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
                modalName: "指标监控",
				uniqueId:'id',
				search: false,
		        showExport: true,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'id', 
					title : 'id',
					visible: false
				},
				{
					field : 'sn', 
					title : '批次号',
					sortable: true
				},
				{
					field : 'planName', 
					title : '计划名称',
					sortable: true
				},
				{
					field : 'startTime', 
					title : '开始时间',
					formatter: function (value) {
						return $.common.formatTime(value);
					}
				},
				{
					field : 'endTime', 
					title : '结束时间',
					formatter: function (value, row) {
						if(row.status==2 || row.status==3){
							return $.common.formatTime(value);
						}else {
							return '';
						}

					}
				},
				{
					field : 'status', 
					title : '执行状态',
					sortable: true,
					formatter: function (value, row ) {
						let hdiv='';
						if (value == 0) {
							hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon " style="color: rgba(175,155,173,0.51);"> 未开始</span>';
						} else if (value == 1) {
							hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon " style="color: rgb(199,198,45);"> 执行中</span>';
						} else if (value == 2) {
							hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon " style="color: rgba(66,220,21,0.96);"> 执行成功</span>';
						} else if (value == 3) {
							hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon " style="color: rgb(255,39,61); font-size: 14px;"> 执行失败</span>';
						}
						return '<font id="statusFont' + row.id + '">' + hdiv + '</font>';
					}
				},
				{
					field : 'hits', 
					title : '并发数',
					sortable: true,
					formatter: function (value) {
						return value+"/s";
					}
				},
				{
					field : 'tps', 
					title : 'TPS',
					sortable: true,
					formatter: function (value) {
						return value+"/s";
					}
				},
				{
					field : 'avgRespTime', 
					title : '平均响应时间',
					formatter: function (value) {
						return value+"(ms)";
					}
				},
				{
					field : 'errorRatio', 
					title : '错误率',
					sortable: true,
					formatter: function (value) {
						return value+"%";
					}
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="seeGraphReport(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看图形报告</a> ');
		            	if(!(row.status==0 || row.status==1)){
							actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
						}
						return actions.join('');
		            }
		        }]
            };

            $.table.init(options);
        });


		setInterval(function () {

			let rows = $('#bootstrap-table').bootstrapTable('getData');
			let runningPlans=rows.filter(it=>(it.status==1||it.status==0)).map(item=>item.id);

			if(runningPlans.length>0){

				$.ajax({
					url: prefix + "/listByIds",
					type: "post",
					dataType: "json",
					contentType:"application/json",
					data: JSON.stringify(runningPlans),
					beforeSend: function () {
					},
					success: function(result) {
						$.each(result,function (index,item) {
							$('#bootstrap-table').bootstrapTable('updateByUniqueId', {
								id: item.id,
								row: {
									Id: item.id,
									tps:item.tps,
									hits: item.hits,
									avgRespTime:item.avgRespTime,
									errorRatio:item.errorRatio,
									status: item.status
								}
							});

						})
					}
				});
			}

		},1000);

        function seeGraphReport(id) {
			createMenuItem(prefix+'displayGraph?id='+id,'图形报告');
		}
    </script>
</body>
</html>